<?php
	session_start();

	$pilots_list = $_SESSION['pilots_list'];
	$prev = $_REQUEST['id'];

	if($_REQUEST['modo'] == "carga"){
		//Conexión a la bd
		$link = mysqli_connect('localhost', 'root', '') or die("Failed to connect to MySQL: " . mysql_error());
		if (mysqli_connect_errno()) {
		        echo 'Failed to connect to MySQL';
		        exit;
		}
		$sql = "USE web_db";
		$query = mysqli_query($link,$sql);

		$sql = "SELECT st.vorname, st.name, me.mec_id, st.id
				FROM staff st, mecha me
				WHERE st.id = me.id";
		$query = mysqli_query($link,$sql);

		//Añade el piloto recibido a la lista de elegidos
		$pilots_list[$prev] = "yes";

		while($row = mysqli_fetch_row($query)){
			if(array_key_exists($row[2], $pilots_list)){
				//Ya se ha elegido ese piloto
				//echo "Mecha " . $row[2] . " elegido.<br>";
			}else{
				echo "<option value=\"" . $row[2] . "\">" . $row[2] . " - " . $row[1] . ", " . $row[0] . "</option>";
			}
		}
	}else{
		unset($pilots_list[$prev]);
	}

	 $_SESSION['pilots_list'] = $pilots_list;
?>